<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<script>
    //2021-06-07-白-各种排序算法  

//示例1 
    //sort() 数组对象排序 其原理是冒泡排序，即比较相邻的元素，如果第一个比第二个大，就交换他们两个。
        var arr = [
            {name:"小恭",age:11},
            {name:"小发",age:3},
            {name:"小喜",age:12},
            {name:"小财",age:40}
         ];
         var compare = function(obj1,obj2){
             var val1 = obj1.name;
             var val2 = obj2.name;
             if(val1 < val2){
                return 2
             }else if(val1>val2){
                 return -1
             }else{
                 return 0
             }
         }

         var sortArr = arr.sort( compare)
         console.log(sortArr)
         console.log(sortArr[0]);  //查看第一个     
         /*
            0: {name: "小财", age: 40}
            1: {name: "小恭", age: 11}
            2: {name: "小喜", age: 12}
            3: {name: "小发", age: 3}
            length: 4
            __proto__: Array(0)
            排序.html:37 
            {name: "小财", age: 40}
            age: 40
            name: "小财"
            __proto__: Object
         */

//示例2  
         //数组 数字排序 
         var arr=[1,4,8,2,11,7,3,5];
         arr.sort(function (a,b){ return a-b });
         //arr = [1,2,3,4,5,7,8,11]


//示例3 
         const arrA = [1,2,2,4,5,35]
         const arrAA = arr.sort(()=>Math.random() - 0.5)
         console.log(arrAA)  //每一次都不一样的 


         

</script>